package cn.ulc.socket.admin.controller;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import cn.ulc.common.constant.PubDataConstant;
import cn.ulc.common.util.Result;
import cn.ulc.socket.admin.model.dto.pubData.PubDataListDTO;
import cn.ulc.socket.admin.model.param.common.PubDataSaveParam;
import cn.ulc.socket.admin.service.pubData.PubDataServ;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

@Validated
@RestController
@RequestMapping(value = "/pubDatas")
public class PubDataCtrl {

    private static final Log log = LogFactory.get();

    @Autowired
    PubDataServ pubDataServ;


    @GetMapping("/dataTypeList")
    public Result findDataTypeList(){
        return Result.success(PubDataConstant.getDataType());
    }

    @GetMapping("/{type}/list")
    public Result findTerminalUserList(@PathVariable @NotNull Integer type){
        PubDataListDTO dto = pubDataServ.findListByType(type);
        return Result.success(dto);
    }

    @DeleteMapping("/{id}")
    public Result deleted(@PathVariable("id") Long id) {
        return Result.success( pubDataServ.deleted(id));
    }

    @PostMapping("/saveDetail")
    public Result save(@Validated @RequestBody PubDataSaveParam param, BindingResult result) {
        if(result.hasErrors()) {
            log.error("保存码表详情参数错误",result.getFieldError().getDefaultMessage());
            return Result.fail(result.getFieldError().getDefaultMessage());
        }
        return Result.success(pubDataServ.saveDetail(param));
    }

}
